import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wan_android_flutter/pages/knowledge/knowledge_vm.dart';

import '../../../common_ui/common_style.dart';
import '../../../common_ui/smart_refresh/smart_refresh_widget.dart';
import '../../../common_ui/web/webview_page.dart';
import '../../../common_ui/web/webview_widget.dart';
import '../../../repository/datas/knowledge_detail_list_model.dart';
import '../../../routes/RouteUtils.dart';
import 'knowledge_detail_vm.dart';

class KnowledgeTabChildPage extends StatefulWidget {
  final String? cid;

  const KnowledgeTabChildPage({super.key, this.cid});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _KnowledgeTabChildPageState();
  }

}

class _KnowledgeTabChildPageState extends State<KnowledgeTabChildPage> {
  KnowledgeDetailTabviewModel viewModel = KnowledgeDetailTabviewModel();
  RefreshController controller = RefreshController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    refreshOrLoadMore(false);
  }
  void refreshOrLoadMore(bool loadMore){
    viewModel.getDetailList(loadMore, widget.cid,callBack: (flag){
      if(flag){
        controller.loadComplete();
      }else{
        controller.refreshCompleted();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ChangeNotifierProvider(create: (context) {
      return viewModel;
    },
      child: Scaffold(body: Consumer<KnowledgeDetailTabviewModel>(
          builder: (context, vm, child) {
            return SmartRefreshWidget(
              controller:controller,
              onRefresh:(){
                refreshOrLoadMore(false);
              },
              onLoading:(){
                refreshOrLoadMore(true);
              },
              child:ListView.builder(
                  // shrinkWrap: true, //计算屏幕显示高度
                  // physics: NeverScrollableScrollPhysics(), //禁用listView滑动事件
                  itemCount: vm.detailList?.length ?? 0,
                  itemBuilder: (context,index){
                    return _item(vm.detailList?[index],onTap: (){
                      RouteUtils.push(
                          context,
                          WebViewPage(
                            loadResource: vm.detailList?[index].link ?? "",
                            webViewType: WebViewType.URL,
                            showTitle: true,
                            title: vm.detailList?[index].title,
                          ));
                    });
              }),);
          }),),);
  }
  Widget _item(KnowledgeDetailItem? item, {GestureTapCallback? onTap}) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
            margin: EdgeInsets.symmetric(vertical: 8.h, horizontal: 15.w),
            padding: EdgeInsets.all(10.r),
            decoration: BoxDecoration(border: Border.all(color: Colors.black12, width: 0.5.r)),
            child: Column(children: [
              Row(
                children: [
                  normalText(item?.superChapterName),
                  Expanded(child: SizedBox()),
                  Text("${item?.niceShareDate}"),
                ],
              ),
              Text("${item?.title}", style: titleTextStyle15),
              Row(
                children: [
                  normalText(item?.chapterName),
                  Expanded(child: SizedBox()),
                  Text("${item?.shareUser}"),
                ],
              )
            ])));
  }
}